package com.qdu.config;

import com.qdu.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.LinkedList;


@Configuration
public class MvcConfig implements WebMvcConfigurer {
    /**
     * Add handlers to serve static resources such as images, js, and, css
     * files from specific locations under web application root, the classpath,
     * and others.
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pic/**").addResourceLocations("file:d:/upload/");
    }


    /**
     * Add Spring MVC lifecycle interceptors for pre- and post-processing of
     * controller method invocations and resource handler requests.
     * Interceptors can be registered to apply to all requests or be limited
     * to a subset of URL patterns.
     *
     * <mvc:interceptors>
     *     <mvc:interptor>
     *         <mvc:mapping path="/**"/>
     *
     *     </mvc:interptor>
     * </mvc:interceptors>
     *
     *
     *
     * @param registry
     */
    @Bean
    LoginInterceptor qduInterceptor() {
        return new LoginInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        LinkedList<String> excludePathPatterns = new LinkedList<>();
        excludePathPatterns.add("/user/login");
        excludePathPatterns.add("/user/register");
        excludePathPatterns.add("/user/selectposition");
        excludePathPatterns.add("/user/checkname");
        excludePathPatterns.add("/pic/*");
        registry.addInterceptor(qduInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(excludePathPatterns);
    }
}
